import { isFalse } from '@src/util/type';
/* components */
import { 
  BaseSelectUserDepartmentTab,
  BaseSelectUserRoleTab,
  BaseSelectUserServiceProviderTab,
  BaseSelectUserDynamicTabComponent,
  BaseSelectUserProjectTeamTab
} from '@src/component/common/BaseSelectUser/components'
/* hooks */
import { useBaseSelectUserOptions } from '@src/component/common/BaseSelectUser/hooks/options'
/* model */
import { BaseSelectUserDynamicTab, BaseSelectUserErrorMessageEnum, BaseSelectUserMarkTypeEnum, BaseSelectUserTab } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import { useBaseSelectUserStoreOptions } from '@src/component/common/BaseSelectUser/store'
/* store */
import {
  useBaseSelectUserStoreCurrentTabType,
  useBaseSelectUserStoreFetchMarkList,
  useBaseSelectUserStoreMarkList,
  useBaseSelectUserStoreLoadingMap,
  useBaseSelectUserStoreTriggerFetchMarkList
} from '@src/component/common/BaseSelectUser/store'
/* util */
import { message } from '@src/util/message'
import { isNotEmpty } from '@src/util/type'
import { isDepartmentTabByTabType, isRoleTabByTabType, isProjectTeamTabByTabType } from '@src/component/common/BaseSelectUser/util'
/* vue */
import { computed, ComputedRef } from 'vue'
/* types */
import { BaseSelectUserGetMarkListModel } from '@src/component/common/BaseSelectUser/types'

import { t } from '@src/locales'
/** 
 * @description 选人组件 tab 选项卡
*/
function useBaseSelectUserTabs() {
  
  const { markList } = useBaseSelectUserStoreMarkList()
  const { currentTabType, setCurrentTabType } = useBaseSelectUserStoreCurrentTabType()
  
  const { isShowDynamic, DynamicTabName, isTrigger } = useBaseSelectUserOptions()
  
  const BaseSelectUserTabMap: Record<BaseSelectUserMarkTypeEnum, any> = {
    [BaseSelectUserMarkTypeEnum.Tag]: BaseSelectUserDepartmentTab,
    [BaseSelectUserMarkTypeEnum.Role]: BaseSelectUserRoleTab,
    [BaseSelectUserMarkTypeEnum.ServiceProvider]: BaseSelectUserServiceProviderTab,
    [BaseSelectUserMarkTypeEnum.Dynamic]: BaseSelectUserDynamicTabComponent,
    // 触发器选人的动态获取tab
    [BaseSelectUserMarkTypeEnum.Da]: BaseSelectUserDynamicTabComponent,
    [BaseSelectUserMarkTypeEnum.ProjectTeam]: BaseSelectUserProjectTeamTab
  }
  
  const filterMarkList = computed(() => {
    const { options } = useBaseSelectUserStoreOptions()

    let UserDynamicTab = {}
    if(options?.value?.DynamicTabName && !!BaseSelectUserDynamicTab){
      UserDynamicTab = {...BaseSelectUserDynamicTab, name: options?.value?.DynamicTabName}
    }

    if (isShowDynamic.value) {
      return [...markList.value, UserDynamicTab]
    }
    
    return markList.value
  })
  
  const tabs: ComputedRef<BaseSelectUserTab[]> = computed(() => {
    return (
      filterMarkList.value.map(mark => {
        
        return {
          name: mark.type,
          label: mark.name,
          component: BaseSelectUserTabMap[mark.type]
        }
        
      })
    )
  })
  return {
    tabs,
    currentTabType,
    setCurrentTabType
  }
}

/** 
 * @description 获取选人组件 tab 选项卡
*/
function useFetchMarkList() {
  
  const { setMarkList } = useBaseSelectUserStoreMarkList()
  const { setCurrentTabType } = useBaseSelectUserStoreCurrentTabType()
  const { setTabLoading, clearTabLoading } = useBaseSelectUserStoreLoadingMap()
  const { 
    showRole, 
    showDepartment, 
    currentTabType, 
    showServiceProvider, 
    showProjectTeam,
    showTagIfSpUser,
    isTrigger
  } = useBaseSelectUserOptions()
  
  const fetchMarkList = (
    isTrigger.value
      ? useBaseSelectUserStoreTriggerFetchMarkList()
      : useBaseSelectUserStoreFetchMarkList()
  )
  
  const fetchMarkListImpl = () => {
    
    const params: BaseSelectUserGetMarkListModel = {
      showSp: showServiceProvider.value,
      showTagIfSpUser: showTagIfSpUser.value
    }
    
    setTabLoading()
    
    return (
      fetchMarkList(params).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          let markList = result?.data || []

          const translate = {
            // 按部门
            tag: t('common.base.allowDepartment'),
            // 按角色
            role: t('common.base.allowRole'),
            // 服务商
            sp: t('common.base.serviceProvider'),
            // 动态获取
            da: t('common.base.dynamicGain'),
            // 动态获取
            dynamic: t('common.base.dynamicGain'),
            projectTeam: t('common.base.allowProjectTeam')
          }
          markList.forEach(mark => {
            // 角色 tab 选项卡数据翻译
            mark.name = translate[mark.type]
          })
          
          markList = markList.filter(mark => {
            // 不显示 角色 tab 选项卡，则过滤掉角色选项卡
            if (isFalse(showRole.value) && isRoleTabByTabType(mark.type)) {
              return false
            }
            
            // 不显示 部门 tab 选项卡，则过滤掉角色选项卡
            if (isFalse(showDepartment.value) && isDepartmentTabByTabType(mark.type)) {
              return false
            }
            
            // 不显示 项目组tab选项卡
            if (isFalse(showProjectTeam.value) && isProjectTeamTabByTabType(mark.type)) {
              return false
            }
            return true
          })
          
          setMarkList(markList)
          
          // 标签选项卡列表不为空，则默认选中第一个
          if (isNotEmpty(markList)) {
            setCurrentTabType(markList[0]?.type)
            // 解决默认选中第一个tab初始化问题
            setTimeout(() => {
              if (currentTabType.value) {
                setCurrentTabType(currentTabType.value)
              }
            }, 100)
          }else{
            // 解决默认选中第一个tab初始化问题
            setTimeout(() => {
              if (currentTabType.value) {
                setCurrentTabType(currentTabType.value)
              }
            }, 100)
          }
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.MarkList)
        }
        
      }).finally(() => {
        clearTabLoading()
      })
    )
  }
  
  return fetchMarkListImpl
}

export {
  useBaseSelectUserTabs,
  useFetchMarkList
}
